package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.Completed;
import com.zygh.policeasset.service.CompletedSerivce;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/completed"})
@Api(tags = {"竣工管理"})
@RestController
public class CompleteController {
    @Autowired
    CompletedSerivce completedSerivce;

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    @ApiOperation("获取竣工分页列表")
    public Object findAll(@RequestParam(required = false) String name,
                          @RequestParam(value = "status",required = false) Integer status,
                          @RequestParam(value = "companyId", required = false) Integer companyId,
                          @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                          @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize,
                          @RequestParam(required = false) String time1,
                          @RequestParam(required = false) String time2,
                          @RequestParam(required = false) Integer category,
                          @RequestHeader("findAll") boolean findAll,
                          @RequestHeader("userId") Integer userId) {
        if (findAll) {
            return this.completedSerivce.findAll(name, status, companyId, pageNum, pageSize, time1, time2, category, null);
        }
        return this.completedSerivce.findAll(name, status, companyId, pageNum, pageSize, time1, time2, category, userId);
    }

    @RequestMapping(value = {"/status"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "0未提交  2已提交", required = true, paramType = "query"), @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "页数", required = true, paramType = "query")})
    @ApiOperation("根据状态获取竣工分页列表")
    public Object getByStatus(@RequestParam int status, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        if (StrUtil.isEmpty(String.valueOf(status))) {
            return new CommonResult().validateFailed("状态不能为空");
        }
        return this.completedSerivce.findByStatus(status, pageNum, pageSize);
    }

    @RequestMapping(value = {"/getproject"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加项目")
    public Object getProject(@RequestHeader("findAll") boolean findAll, @RequestHeader("userId") Integer userId) {
        if (findAll) {
            return this.completedSerivce.getProject(null);
        }
        return this.completedSerivce.getProject(userId);
    }

    @RequestMapping(value = {"/getadd"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加合同")
    public Object getAdd(@RequestParam String projectNo, @RequestHeader("findAll") boolean findAll, @RequestHeader("userId") Integer userId) {
        if (StrUtil.isEmpty(projectNo)) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (findAll) {
            return this.completedSerivce.getAdd(projectNo, null);
        }
        return this.completedSerivce.getAdd(projectNo, userId);
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加竣工")
    public Object add(@RequestBody Completed completed) throws ParseException {
        if (StrUtil.isEmpty(completed.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (StrUtil.isEmpty(completed.getProjectName())) {
            return new CommonResult().validateFailed("项目名称不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(completed.getSectionId()))) {
            return new CommonResult().validateFailed("标段id不能为空");
        }
        if (completed.getProjectNo().contains("ZN") && !completed.getInitialTime().isEmpty() && !completed.getFinalTime().isEmpty()) {
            SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd");
            if (sdff.parse(completed.getFinalTime()).before(sdff.parse(completed.getInitialTime()))) {
                return new CommonResult().validateFailed("终验时间早于初验时间");
            }
        }
        if (this.completedSerivce.add(completed) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @DeleteMapping({"/remove/{id}"})
    @ApiImplicitParam(name = "id", value = "唯一主键", required = true, paramType = "query")
    @ApiOperation("删除")
    public Object delete(@PathVariable Integer id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.completedSerivce.delete(id) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败");
    }

    @RequestMapping(value = {"/selectbyid"}, method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "唯一主键", required = true, paramType = "query")
    @ApiOperation("通过ID查询")
    public Object selectById(Integer id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        return new CommonResult().success(this.completedSerivce.selectById(id));
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("更新")
    public Object update(@RequestBody Completed completed) {
        if (StrUtil.isEmpty(String.valueOf(completed.getId()))) {
            return new CommonResult().validateFailed("主键不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(completed.getSectionId()))) {
            return new CommonResult().validateFailed("标段id不能为空");
        }
        if (this.completedSerivce.update(completed) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("更新失败");
    }
}